package org.dtp.config;

import org.dtp.enums.BlockQueueEnum;
import org.dtp.enums.RejectedExecutionHandlerEnum;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ThreadPoolProperties
 * @Description 线程池配置
 * @Author wangyiqi
 * @Date 2021-08-11-20:22
 **/
public class ThreadPoolProperties {
    /**
     * 线程池名称
     */
    private String threadPoolName = "DynamicThreadPool";

    /**
     * 核心线程数
     */
    private int corePoolSize = 1;

    /**
     * 最大线程数, 默认值为CPU核心数量
     */
    private int maximumPoolSize = Runtime.getRuntime().availableProcessors();

    /**
     * 空闲线程存活时间
     */
    private long keepAliveTime;

    /**
     * 空闲线程存活时间单位
     */
    private TimeUnit unit = TimeUnit.MILLISECONDS;

    /**
     * 队列最大数量
     */
    private int queueCapacity = Integer.MAX_VALUE;

    /**
     * 队列类型
     */
    private String queueType = BlockQueueEnum.LINKED_BLOCKING_QUEUE.type();

    /**
     * 拒绝策略
     */
    private String rejectedExecutionType = RejectedExecutionHandlerEnum.ABORT_POLICY.type();

    /**
     * SynchronousQueue 是否公平策略，默认非公平
     */
    private boolean fair = false;

    /**
     * 队列容量阀值，超过此值告警
     */
    private int queueCapacityThreshold = queueCapacity;

    public String getThreadPoolName() {
        return threadPoolName;
    }

    public void setThreadPoolName(String threadPoolName) {
        this.threadPoolName = threadPoolName;
    }

    public int getCorePoolSize() {
        return corePoolSize;
    }

    public void setCorePoolSize(int corePoolSize) {
        this.corePoolSize = corePoolSize;
    }

    public int getMaximumPoolSize() {
        return maximumPoolSize;
    }

    public void setMaximumPoolSize(int maximumPoolSize) {
        this.maximumPoolSize = maximumPoolSize;
    }

    public long getKeepAliveTime() {
        return keepAliveTime;
    }

    public void setKeepAliveTime(long keepAliveTime) {
        this.keepAliveTime = keepAliveTime;
    }

    public TimeUnit getUnit() {
        return unit;
    }

    public void setUnit(TimeUnit unit) {
        this.unit = unit;
    }

    public int getQueueCapacity() {
        return queueCapacity;
    }

    public void setQueueCapacity(int queueCapacity) {
        this.queueCapacity = queueCapacity;
    }

    public String getQueueType() {
        return queueType;
    }

    public void setQueueType(String queueType) {
        this.queueType = queueType;
    }

    public String getRejectedExecutionType() {
        return rejectedExecutionType;
    }

    public void setRejectedExecutionType(String rejectedExecutionType) {
        this.rejectedExecutionType = rejectedExecutionType;
    }

    public boolean isFair() {
        return fair;
    }

    public void setFair(boolean fair) {
        this.fair = fair;
    }

    public int getQueueCapacityThreshold() {
        return queueCapacityThreshold;
    }

    public void setQueueCapacityThreshold(int queueCapacityThreshold) {
        this.queueCapacityThreshold = queueCapacityThreshold;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ThreadPoolProperties that = (ThreadPoolProperties) o;
        return corePoolSize == that.corePoolSize &&
                maximumPoolSize == that.maximumPoolSize &&
                keepAliveTime == that.keepAliveTime &&
                queueCapacity == that.queueCapacity &&
                fair == that.fair &&
                queueCapacityThreshold == that.queueCapacityThreshold &&
                Objects.equals(threadPoolName, that.threadPoolName) &&
                unit == that.unit &&
                Objects.equals(queueType, that.queueType) &&
                Objects.equals(rejectedExecutionType, that.rejectedExecutionType);
    }

    @Override
    public int hashCode() {
        return Objects.hash(threadPoolName, corePoolSize, maximumPoolSize, keepAliveTime, unit, queueCapacity, queueType, rejectedExecutionType, fair, queueCapacityThreshold);
    }

    @Override
    public String toString() {
        return "ThreadPoolProperties{" +
                "threadPoolName='" + threadPoolName + '\'' +
                ", corePoolSize=" + corePoolSize +
                ", maximumPoolSize=" + maximumPoolSize +
                ", keepAliveTime=" + keepAliveTime +
                ", unit=" + unit +
                ", queueCapacity=" + queueCapacity +
                ", queueType='" + queueType + '\'' +
                ", rejectedExecutionType='" + rejectedExecutionType + '\'' +
                ", fair=" + fair +
                ", queueCapacityThreshold=" + queueCapacityThreshold +
                '}';
    }
}
